package com.tianhui.consignortwofei.mvp.ui.activity.audit;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;

import androidx.annotation.Nullable;

import com.bm.library.PhotoView;
import com.fgs.common.entity.PictureInfo;
import com.fgs.common.entity.UploadPictureInfo;
import com.fgs.common.imageView.strategy.ImageLoader;
import com.fgs.common.pictureSelector.SelectPicture;
import com.fgs.common.uploadPicture.IUploadPicture;
import com.fgs.common.uploadPicture.UploadPicture;
import com.fgs.common.utils.ImageUtil;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.base.BaseActivity;
import com.tianhui.consignortwofei.other.auth.AuthInfoProcess;
import com.tianhui.consignortwofei.other.auth.PictureContext;
import com.tianhui.consignortwofei.other.auth.PictureStrategyFactory;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author fgs
 * @date 2020/12/25
 * @description
 **/
public abstract class BaseAuditActivity extends BaseActivity {
    @BindView(R.id.layout_audit_picture_photoView)
    protected PhotoView mPhotoView;

    private SelectPicture mSelectPicture;
    protected PictureStrategyFactory pictureStrategyFactory;
    protected PictureContext pictureContext;
    protected AuthInfoProcess mAuthInfoProcess;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPhotoView.enable();
        mSelectPicture = new SelectPicture();
        mSelectPicture.setCrop(true);
//        mSelectPicture.setAspectRatio(SelectPicture.RATIO_1_1);
        pictureStrategyFactory = new PictureStrategyFactory();
        mAuthInfoProcess = new AuthInfoProcess();
    }


    public void loadPicture(String pictureId) {
//        ImageLoader.init().loadImageWithId(mPhotoView, pictureId);
        ImageLoader.init().loadImageWithId(mPhotoView, pictureId);
    }

    public void loadPicture(String pictureId, boolean isNeedCache) {
        ImageLoader.init().loadImageWithId(mPhotoView, pictureId, isNeedCache);
    }

//    @OnClick(R.id.layout_audit_picture_editImageView)
//    public void changePicture() {
//        mSelectPicture.selectSinglePicture(this, new SelectPicture.OnSelectResultCallback() {
//            @Override
//            public void selectResultCallback(List<LocalMedia> selectList) {
//            }
//
//            @Override
//            public void selectResultPath(String path) {
//                requestUploadPicture(path);
//            }
//
//            @Override
//            public void deletePictureCallback(List<LocalMedia> selectList, int position) {
//
//            }
//        });
//    }

    @OnClick(R.id.layout_audit_picture_downloadImageView)
    public void downloadPicture() {
        Bitmap bitmap = ((BitmapDrawable) (mPhotoView).getDrawable()).getBitmap();
        if (bitmap != null) {
            ImageUtil.saveImageToGallery(this, bitmap);
            showToast("保存成功");
        } else {
            showToast("保存失败");
        }
    }

    private void requestUploadPicture(String path) {

        UploadPictureInfo uploadPictureInfo = new UploadPictureInfo();
        uploadPictureInfo.pictureModule = getPictureModule();
        uploadPictureInfo.picturePath = path;
        new UploadPicture(new IUploadPicture.UploadPictureCallback() {
            @Override
            public void onSuccess(UploadPictureInfo uploadPictureInfo, PictureInfo pictureInfo) {
                loadPicture(pictureInfo.id);
                changePictureSuccess(pictureInfo);
            }

            @Override
            public void onFailed(UploadPictureInfo uploadPictureInfo) {

            }
        }).uploadPicture(this, uploadPictureInfo);

    }

    public abstract String getPictureModule();

    public abstract void changePictureSuccess(PictureInfo pictureInfo);

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (pictureContext != null)
            pictureContext.onActivityResult(requestCode, resultCode, data);
    }
}
